<?php
namespace User\Controller;
use Think\Controller;
class FileMangerController extends CommonController {

    public function updateField($value='',$pk = '',$name = '')
    {
        if (!$name || !$pk  ) return;
        if ($name == 'icon') {
            $appath = 'User/'.numberDir($this->apid).'icon/content/';
            $appicon = $appath.$pk.'.png';
            if (!is_dir($appath)) {
                mkdir($appath,0777,true);
            }
            @copy($value, $appicon);
            $value = $appicon;
        }

        if ($name == 'picurl') {
            $appath = 'User/'.numberDir($this->apid).'appbg/content/';
            $appicon = $appath.$pk.'.png';
            if (!is_dir($appath)) {
                mkdir($appath,0777,true);
            }
            @copy($value, $appicon);
            $value = $appicon;
        }


        $model = M( $this->con );
        $map['id'] = $pk;
        $map['mid'] = $this->mid;
        $data[$name] = $value;
        $model->where($map)->save($data);


        $this->success( '成功!');
    }

    public function markerAppicon($bg = '',$txt = '')
    {
        $bg = urldecode($bg);
        $appath = 'User/'.numberDir($this->apid).'icon/';
        $appicon = $appath.'icon.png';
        if (!is_dir($appath)) {
            mkdir($appath,0777,true);
        }
        $ret = copy($bg, $appicon);
        $WaterMask = new \Common\ORG\WaterMask($appicon);
        $WaterMask->waterStr = $txt;
        $WaterMask->pos = 5;
        $WaterMask->fontSize = 60;
        $WaterMask->output(); 

        $map['mid'] = $this->mid;
        $map['id'] = $this->apid;
        $Apps = M('Apps');
        $data['icon'] = $appicon;
        $Apps->where($map)->save($data);
    }

    public function index($page = 1)
    {
        $Attr = D('Attr');
        $map['mid'] = $this->mid;
        $attrs = $Attr->getPage($map);
        $this->assign('attrs', $attrs);

        $icons = $this->sc('icon');
        $this->assign('icons', $icons);

        $bgs = $this->sc('bg');
        $this->assign('bgs', $bgs);

        $this->display();
    }

    public function sc($t ='icon')
    {
        $dir = 'resource/';
        $dir = $dir.$t.'/';
        $f = scandir($dir);
        $data = array();
        foreach ($f as $k) {
            if ($k == '.') continue;
            if ($k == '..') continue;
            if ($k == 'full') continue;
            $vo['fname'] = $k;
            $vo['fpath'] = $dir.$k;
            $data[] = $vo;
        }
        return $data;

    }

    public function upfile()
    {
        // file_data
        $upload = new \Think\Upload();

        $upload->subName   =     array('date','Y/m/d');
        $upload->autoSub   =     true ;
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        $info   =   $upload->upload();
        $Attr = M('Attr');
        $url = U('Upload/delete');

        if(!$info) {
            $this->error($upload->getError());
        }else{
            $ret =  array();
            $ret['append'] = "true";
            foreach($info as $f){

                $fp = 'Uploads/'.$f['savepath'].$f['savename'];
                $data['mid'] = $this->mid;
                $data['appid'] = $_GET['apid'];
                $data['fpath'] = $fp;
                $data['originalName'] = $f['name'];
                $data['suffix'] = $f['ext'];
                $data['url'] = 'http://'.$_SERVER['SERVER_NAME'].'/'.$fp;
                $data['ctime'] = time();
                $data['filename'] = $f['savename'];
                $data['dirpath'] = 'Uploads/'.$f['savepath'];
                $ip[] = "<img src='".$fp."' class='file-preview-image'>";
                $pk = $Attr->add($data);
                $ipc[]['url'] = U("Upload/fileInputDelete?appid=".$this->appid.'&id='.$pk);
                $ipc[]['key'] = $pk;
            }
            $ret['fp'] = $fp;
            $ret['initialPreview'] = $ip;
            $ret['initialPreviewConfig'] = $ipc;
            $this->ajaxReturn($ret);
        }
    }

    public function fileInputDelete($id='')
    {
        $Attr = M('Attr');
        $map['mid'] = $this->mid;
        $map['id'] = $id;
        $v = $Attr->where($map)->find();
        $ret = $Attr->where($map)->delete();
        if ($ret !== false) {
            unlink($v['fpath']);
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    public function deletPath($fpath ='')
    {
        $Attr = M('Attr');
        $map['mid'] = $this->mid;
        $map['fpath'] = $fpath;
        $v = $UserAttr->where($map)->find();
        $ret = $UserAttr->where($map)->delete();
        if ($ret !== false) {
            unlink($v['fpath']);
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}